package com.dxf.redis.sorted;

import com.dxf.redis.distributedlock.RedisLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class RedisSorted {

    private static final Logger log = LoggerFactory.getLogger(RedisLock.class);

    static {
        jedis = new Jedis("192.168.10.113", 6379);

    }
    private static final Jedis jedis;

    public Double addSore(String code,Double score){

        jedis.select(2);
      return    jedis.zincrby("TV_RANKING",score,code);

    }

    public List<String> getLastN(Long limit) {
        jedis.select(2);
        if (limit <= 0) {
            throw new RuntimeException();
        }
        return jedis.zrange("TV_RANKING", 0, limit - 1); //正序
    }

    public List<String> getTopN(Long limit) {
        jedis.select(2);
        if (limit <= 0) {
            throw new RuntimeException();
        }
        return jedis.zrevrange("TV_RANKING", 0, limit - 1); //正序
    }

    public static void main(String[] args) {
        RedisSorted redisSorted = new RedisSorted();
       /* redisSorted.addSore("北京卫视",1D);
        redisSorted.addSore("北京卫视",1D);
        redisSorted.addSore("湖南卫视",1D);
        redisSorted.addSore("江苏卫视",1D);*/
        List<String> topN = redisSorted.getTopN(2L);

        log.info("开启排序操作！！！！！");
        jedis.select(2);
        for (String i: topN) {
            System.out.println(i+"积分："+jedis.zscore("TV_RANKING",i));

        }

        log.info("结束排序操作！！！！！");



    }
}
